﻿using System;
using System.Collections.Generic;
using System.Data;
using Titan.WinFormSys;
using Infragistics.Win.UltraWinGrid;
using Titan.FIS.Enum;
using Titan.FIS.UI;
using System.Windows.Forms;
using System.Reflection;

namespace Titan.FIS.LmsBalanceCollect
{
    public partial class frmMain : FrmBase  //Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public frmMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_Load(object sender, EventArgs e)
        {
            Common.UserInfo = UserInfo;
            Common.DataTable = tabData;
            Common.GridMain = utg_Main;
            Common.MsgBox = ricMsg;

            //UltraGrid自适应宽度
            utg_Main.DisplayLayout.Bands[0].PerformAutoResizeColumns(false, PerformAutoSizeType.AllRowsInBand);
        }

        /// <summary>
        /// 表格点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void utg_Main_Click(object sender, EventArgs e)
        {
            if (utg_Main.ActiveRow != null)
            {
                ute_AsNo.Text = utg_Main.ActiveRow.Cells["TEST_CODE"].Value.ToString();
                //utg_Main.ActiveRow.Activated = false;
            }
        }

        /// <summary>
        /// 化验号点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ute_AsNo_Click(object sender, EventArgs e)
        {
            ute_AsNo.Text = "";
        }

        /// <summary>
        /// 按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ultraTool_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "Query":
                    DoQuery("Normal");
                    break;
                case "Collect":
                    DoCollect();
                    break;
                case "Set":
                    DoSet();
                    break;
            }
        }

        /// <summary>
        /// 查询称重信息
        /// </summary>
        private void DoQuery(string type)
        {
            var strAsNo = ute_AsNo.Text.Trim();
            var strStartTime = Convert.ToDateTime(ute_Start.Value).ToString("yyyy-MM-dd") + " 00:00:00";
            var strEndTime = Convert.ToDateTime(ute_End.Value).ToString("yyyy-MM-dd") + " 23:59:59";

            DataTable tabSource = null;
            DataTable dt = null;
            var errMsg = DataQuery.DoQueryTestDataInfo(strAsNo,  strStartTime, strEndTime, ref tabSource);
            if (string.IsNullOrEmpty(errMsg) && tabSource != null)
            {
                dt = tabSource.Copy();
            }
            else  
            {
                FormHelper.ApplicationShow(ricMsg, DisplayType.Error, $"查询数据失败!，{errMsg}");
            }
            if (type == "Normal")
                FormHelper.ApplicationShow(ricMsg, DisplayType.Run, @"查询完毕！");

            if (dt != null && dt.Rows.Count == 0)//查询到的数据为0直接退出
            {
                utg_Main.DataSource = tabData;
                return;
            }
            if (dt != null)
            {
                foreach (DataRow dataRow in dt.Rows)
                {
                    dataRow["INSTRUMENT"] = EnumClass.GetEnumDes((LaboratoryInstrument)int.Parse(dataRow["INSTRUMENT"].ToString()));
                }
                Titan.FIS.Data.MergeDataTable.MergeCaption(tabData, ref dt);
                utg_Main.DataSource = dt;
            }
            //UltraGrid自适应宽度
            utg_Main.DisplayLayout.Bands[0].PerformAutoResizeColumns(false, PerformAutoSizeType.AllRowsInBand);
            utg_Main.ActiveRow = null;
        }

        private frmLmsTest frmLmsTest = null;
        /// <summary>
        /// 开始称重
        /// </summary>
        private void DoCollect()
        {
            if (!frmLmsTest._isOpen)
            {
                frmLmsTest = new frmLmsTest();
                frmLmsTest.Show();
            }
            else
            {
                if (frmLmsTest.WindowState == FormWindowState.Minimized)
                    frmLmsTest.WindowState = FormWindowState.Maximized;
                frmLmsTest.Activate();
            }
        }

        /// <summary>
        /// com设置
        /// </summary>
        private void DoSet()
        {
            var obj = new frmSet();
            obj.ShowDialog();
        }


    }
}
